package com.playball.lottery.fragment.frm;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.eventmanger.EventManager;
import com.playball.lottery.eventmanger.EventType;
import com.playball.lottery.frm.set.SetMainActivity;
import com.playball.lottery.frm.yhzx.AccountDetailActivity;
import com.playball.lottery.frm.yhzx.BetRecordActivity;
import com.playball.lottery.frm.yhzx.MyCmdQRCode;
import com.playball.lottery.frm.yhzx.MyHuiYuan;
import com.playball.lottery.frm.yhzx.MyHuiYuanRecarActivity;
import com.playball.lottery.frm.yhzx.MyYongJin;
import com.playball.lottery.frm.yhzx.SavedPlanActivity;
import com.playball.lottery.frm.yhzx.SecurityCenterActivity;
import com.playball.lottery.frm.yhzx.WithDrawalActivity;
import com.playball.lottery.frm.yhzx.ZhRecordActivity;
import com.playball.lottery.nt.GetJsonData;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.sessions.SessionManager;
import com.playball.lottery.util.ConvertJsonToList;
import com.playball.lottery.util.LotConstants;


public class UserCenterFragment extends Fragment implements LotConstants {
	public static final String TAG = "UserCenterFragment";
	View _RootView;

	private TextView tv_title;
	private TextView btn_title_left;
	private TextView btn_title_right;
	private Button btn_recharge;
	private Button btn_withdraw;
	private TextView tv_userName;
	private View view_betrecord;
	private View view_account_detail;
	private View view_account_zh_record;
	private View view_account_security;
	private View view_account_saved_plan;
	private View view_cmd_cmdurl,view_cmd_cmd_recharge;
	private View view_cmd_myhuiyuan;
	private View view_cmd_myyongjin;
	// 幸运选好
	private View viewLucky;
	// 比分直播
	private View viewScore;

	private View user_name_par;
	private View yu_e_par,view_unino;

	Button btn_logout;

	TextView balance;
	SharedPreferences sp;
	private ImageView rank_img;
	private TextView if_zj_text;
	private Boolean ifWin = false;

	private Button jdd_uc_btn_hongbao;

	SessionManager session;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		_RootView = inflater.inflate(R.layout.usercenter_layout, null);
		return _RootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		session = new SessionManager(getContext());

		view_unino = _RootView.findViewById(R.id.view_unino);
		tv_title = (TextView) _RootView.findViewById(R.id.top_title_info);
		btn_title_left = (TextView) _RootView.findViewById(R.id.top_title_back);
		btn_title_right = (TextView) _RootView.findViewById(R.id.top_title_right_btn);
		btn_recharge = (Button) _RootView.findViewById(R.id.btn_recharge);
		btn_withdraw = (Button) _RootView.findViewById(R.id.btn_withdraw);
		tv_userName = (TextView) _RootView.findViewById(R.id.tv_user_name);
		btn_logout = (Button) _RootView.findViewById(R.id.jdd_uc_btn_logout);
		view_betrecord = _RootView.findViewById(R.id.view_betrecord);
		view_account_detail = _RootView.findViewById(R.id.view_account_detail);
		view_account_zh_record = _RootView.findViewById(R.id.view_account_zh_record);
		view_account_security = _RootView.findViewById(R.id.view_account_security);
		view_account_saved_plan = _RootView.findViewById(R.id.view_account_saved_plan);
		rank_img = (ImageView) _RootView.findViewById(R.id.rank_img);
		balance = (TextView) _RootView.findViewById(R.id.tv_Balance);
		if_zj_text = (TextView) _RootView.findViewById(R.id.if_zj_text);
		if (!TextUtils.isEmpty(ApplicationData.getInstance().getUserMoney()))
			balance.setText(ApplicationData.getInstance().getUserMoney());

		sp = getActivity().getSharedPreferences("userNamesFile", Activity.MODE_PRIVATE);

		view_cmd_cmdurl = _RootView.findViewById(R.id.view_cmd_cmdurl);
		view_cmd_cmdurl.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getActivity(), MyCmdQRCode.class);
				getActivity().startActivityForResult(intent, REQ_MYCMDQR_FROM_USER_CENTER_FRM);
			}
		});
		view_cmd_cmd_recharge = _RootView.findViewById(R.id.view_cmd_cmd_recharge);
		view_cmd_cmd_recharge.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getActivity(), MyHuiYuanRecarActivity.class);
				getActivity().startActivityForResult(intent, REQ_MYCMDQR_FROM_USER_CENTER_FRM);
			}
		});
		
		view_cmd_myhuiyuan = _RootView.findViewById(R.id.view_cmd_mycmd);
		view_cmd_myhuiyuan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getActivity(), MyHuiYuan.class);
				getActivity().startActivityForResult(intent, REQ_MYCMDQR_FROM_USER_CENTER_FRM);
			}
		});
		
		view_cmd_myyongjin= _RootView.findViewById(R.id.view_cmd_myyeji);
		view_cmd_myyongjin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getActivity(), MyYongJin.class);
				getActivity().startActivityForResult(intent, REQ_MYCMDQR_FROM_USER_CENTER_FRM);
			}
		});
		
		view_account_saved_plan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), SavedPlanActivity.class);
				getActivity().startActivityForResult(intent, REQ_SAVEDPLAN_LIST_FROM_USER_CENTER_FRM);
			}
		});

		view_account_security.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), SecurityCenterActivity.class);
				getActivity().startActivityForResult(intent, REQ_ZHRECORD_LIST_FROM_USER_CENTER_FRM);
			}
		});

		view_account_zh_record.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), ZhRecordActivity.class);
				getActivity().startActivityForResult(intent, REQ_ZHRECORD_LIST_FROM_USER_CENTER_FRM);
			}
		});

		view_betrecord.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), BetRecordActivity.class);
				getActivity().startActivityForResult(intent, REQ_bETRECORD_LIST_FROM_USER_CENTER_FRM);
				if (ifWin) {
					if_zj_text.setVisibility(View.GONE);
					GetIsWin task1 = new GetIsWin();
					task1.execute("125");
				}
			}
		});
		view_account_detail.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), AccountDetailActivity.class);
				getActivity().startActivityForResult(intent, REQ_ACCOUNT_DETAIL_FROM_USER_CENTER_FRM);
			}
		});
		btn_recharge.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent1= new Intent();        
			    intent1.setAction("android.intent.action.VIEW");    
			    Uri content_url = Uri.parse(GetJsonData.urlPath_recharge+ApplicationData.getInstance().getUserID());   
			    intent1.setData(content_url);  
			    startActivity(intent1);
				
			}
		});

		btn_withdraw.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), WithDrawalActivity.class);
				getActivity().startActivityForResult(intent, REQ_WITHDRAWAL_LIST_FROM_USER_CENTER_FRM);
			}
		});

		btn_title_left.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EventManager.getInstance().onEvent(EventType.EV_UI_BLL_SCROLL_TO_LEFT, null);
			}
		});

		tv_title.setText("账户中心");

		// btn_title_left.setBackgroundResource(R.drawable.top_title_right_bg);
		btn_title_left.setVisibility(View.INVISIBLE);

		btn_title_right.setVisibility(View.VISIBLE);
		btn_title_right.setText("更多");
		btn_title_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(getActivity(), SetMainActivity.class);
				startActivity(it);
			}
		});


		
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onResume() {
		super.onResume();

		AsyncLoader task = new AsyncLoader();
		task.execute("");
		GetIsWin task1 = new GetIsWin();
		task1.execute("124");
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		super.onDetach();
	}

	/**
	 * 获取用户基本信息
	 */
	private class AsyncLoader extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			tv_userName.setText(ApplicationData.getInstance().getUserName());
		}

		@Override
		protected String doInBackground(String... params) {
			String action = "107";
			String strResult;

			JSONObject param = new JSONObject();
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(param.toString(), UserID, action, getActivity()
					.getApplicationContext());
			System.err.println("登录成功之后返回的+++++" + strResult);
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			try {
				if (null != result) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();

					if (js.getInt("Code") == 0) {
						Map<String, Object> map = new HashMap<String, Object>();
						map = ConvertJsonToList.getMap(js.getString("Data"));
						int intPicId = 0;
						try {
							ApplicationData.getInstance().setRealName(map.get("RealName").toString());
							ApplicationData.getInstance().setCardNum(map.get("BankCardNumber").toString());
							ApplicationData.getInstance().setMobile(map.get("Mobile").toString());
							ApplicationData.getInstance().setBankName(map.get("BankName").toString());
							ApplicationData.getInstance().setUserMoney(map.get("Balance").toString());
							ApplicationData.getInstance().setIDCardNumber(map.get("IDCardNumber").toString());
							ApplicationData.getInstance().setBandID(map.get("BindID").toString());
							ApplicationData.getInstance().setIsMobileValied(
									map.get("IsMobileValied").toString().equals("true"));
						} catch (Exception e) {
							e.printStackTrace();
						}
						balance.setText(ApplicationData.getInstance().getUserMoney());
						boolean isUnin = Boolean.parseBoolean(map.get("IsUnion").toString());
						if(isUnin){
							view_unino.setVisibility(View.VISIBLE);
						}else{
							view_unino.setVisibility(View.GONE);
						}
						
					}
				}
			} catch (JSONException e) {
				// e.printStackTrace();
			}
		}

	}

	class GetIsWin extends AsyncTask<String, Integer, String> {
		String action;

		@Override
		protected String doInBackground(String... params) {
			// 处理后台执行的任务，在后台线程执行
			String strResult;
			// 设置请求action
			action = params[0];
			JSONObject param = new JSONObject();
			// 获取请求返回值
			strResult = GetRequestParams.getRequestUserIDResult(param.toString(), ApplicationData.getInstance()
					.getUserID(), action, getActivity().getApplicationContext());
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if ("124".equals(action)) {
						if (Integer.parseInt(js.getString("Code")) == 0) {
							if_zj_text.setVisibility(View.VISIBLE);
							ifWin = true;
						} else {
							if_zj_text.setVisibility(View.GONE);
						}
					}
				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {

				}
			}

		}
	}
}
